package com.cloud.job;

import com.alibaba.fastjson.JSON;
import com.cloud.entity.ExceptionLogEntity;
import com.cloud.job.handler.*;
import com.cloud.req.QueryExceptionLogDto;
import com.cloud.service.ExceptionLogService;
import com.cloud.utils.BeanConvertUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;


@Slf4j
@Component
public class HandlerDataSourceJob {

    @Autowired
    private UserJobHandler userJobHandler;
    @Autowired
    private MesSysBarCodeJobHandler mesSysBarCodeJobHandler;
    @Autowired
    private MesPcbBoxCodeBindJobHandler mesPcbBoxCodeBindJobHandler;
    @Autowired
    private MesPcbBarCodeJobHandler mesPcbBarCodeJobHandler;
    @Autowired
    private MesPcbCompleteJobHandler mesPcbCompleteJobHandler;
    @Autowired
    private MesPcbProduceTaskJobHandler mesPcbProduceTaskJobHandler;
    @Autowired
    private MesPmcProduceTaskJobHandler mesPmcProduceTaskJobHandler;
    @Autowired
    private MesProductBindJobHandler mesProductBindJobHandler;
    @Autowired
    private MesQmMaintenanceDtlJobHandler mesQmMaintenanceDtlJobHandler;
    @Autowired
    private MesQmMaintenanceJobHandler mesQmMaintenanceJobHandler;
    @Autowired
    private MesQmProductExamJobHandler mesQmProductExamJobHandler;
    @Autowired
    private MesQmRepairJobHandler mesQmRepairJobHandler;
    @Autowired
    private MesQmTestProcessJobHandler mesQmTestProcessJobHandler;
    @Autowired
    private MesSysBoxCodeBindJobHandler mesSysBoxCodeBindJobHandler;
    @Autowired
    private MesSysJyMaterialJobHandler mesSysJyMaterialJobHandler;
    @Autowired
    private MesPcbProductContrastJobHandler mesPcbProductContrastJobHandler;
    @Autowired
    private MesSysProductContrastJobHandler mesSysProductContrastJobHandler;
    @Autowired
    private MesQmInComingItemJobHandler mesQmInComingItemJobHandler;
    @Autowired
    private ExceptionLogService exceptionLogService;


    @XxlJob("HandlerExceptionLogJob")
    public ReturnT<String> handlerExceptionLogJob(String param) {
        log.info("处理异常数据开始");
        JobParam jobParam = StringUtils.isBlank(param) ? null : JSON.parseObject(param, JobParam.class);
        log.info("定时任务参数：" + JSON.toJSONString(jobParam));

        QueryExceptionLogDto reqDto = BeanConvertUtil.convert(jobParam, QueryExceptionLogDto.class);
        List<ExceptionLogEntity> list = exceptionLogService.queryExceptionLog(reqDto);
        log.info("需要处理的数据：{}", list);

        if (CollectionUtils.isEmpty(list)) {
            log.info("无异常数据需要处理");
            return ReturnT.SUCCESS;
        }

        for (int i = 0; i < list.size(); i++) {
            ExceptionLogEntity exceptionLogEntity = list.get(i);
            // user  是表名
            if ("user".equals(exceptionLogEntity.getTableName())) {
                userJobHandler.execute(exceptionLogEntity);
            } else if ("mes_sys_barcode".equals(exceptionLogEntity.getTableName())) {
                mesSysBarCodeJobHandler.execute(exceptionLogEntity);
            } else if ("mes_pmc_producetask".equals(exceptionLogEntity.getTableName())) {
                mesPmcProduceTaskJobHandler.execute(exceptionLogEntity);
            } else if ("mes_product_bind".equals(exceptionLogEntity.getTableName())) {
                mesProductBindJobHandler.execute(exceptionLogEntity);
            } else if ("mes_pcb_boxcode_bind".equals(exceptionLogEntity.getTableName())) {
                mesPcbBoxCodeBindJobHandler.execute(exceptionLogEntity);
            } else if ("mes_pcb_barcode".equals(exceptionLogEntity.getTableName())) {
                mesPcbBarCodeJobHandler.execute(exceptionLogEntity);
            } else if ("mes_pcb_producetask".equals(exceptionLogEntity.getTableName())) {
                mesPcbProduceTaskJobHandler.execute(exceptionLogEntity);
            } else if ("mes_sys_boxcdebind".equals(exceptionLogEntity.getTableName())) {
                mesSysBoxCodeBindJobHandler.execute(exceptionLogEntity);
            } else if ("mes_qm_testprocess".equals(exceptionLogEntity.getTableName())) {
                mesQmTestProcessJobHandler.execute(exceptionLogEntity);
            } else if ("mes_qm_productexam".equals(exceptionLogEntity.getTableName())) {
                mesQmProductExamJobHandler.execute(exceptionLogEntity);
            } else if ("mes_qm_repair".equals(exceptionLogEntity.getTableName())) {
                mesQmRepairJobHandler.execute(exceptionLogEntity);
            } else if ("mes_qm_maintenance".equals(exceptionLogEntity.getTableName())) {
                mesQmMaintenanceJobHandler.execute(exceptionLogEntity);
            } else if ("mes_qm_maintenance_dtl".equals(exceptionLogEntity.getTableName())) {
                mesQmMaintenanceDtlJobHandler.execute(exceptionLogEntity);
            } else if ("mes_pcb_complete".equals(exceptionLogEntity.getTableName())) {
                mesPcbCompleteJobHandler.execute(exceptionLogEntity);
            } else if ("mes_sys_jy_material".equals(exceptionLogEntity.getTableName())) {
                mesSysJyMaterialJobHandler.execute(exceptionLogEntity);
            } else if ("mes_pcb_product_contrast".equals(exceptionLogEntity.getTableName())) {
                mesPcbProductContrastJobHandler.execute(exceptionLogEntity);
            } else if ("mes_sys_product_contrast".equals(exceptionLogEntity.getTableName())) {
                mesSysProductContrastJobHandler.execute(exceptionLogEntity);
            } else if ("mes_qm_incomingitem".equals(exceptionLogEntity.getTableName())) {
                mesQmInComingItemJobHandler.execute(exceptionLogEntity);
            }
        }
        // 拉取所有的账户,去重
        log.info("处理异常数据结束");
        return ReturnT.SUCCESS;
    }

    // {"tableNames":["user","teacher"],"ids":[11,12],"beginDate":"2023-11-30 11:20","endDate":"2023-12-30 11:20"}
    @Getter
    @Setter
    public static class JobParam {
        /**
         * 表名
         */
        private List<String> tableNames;

        /**
         * 开始时间
         */
        private String beginDate;

        /**
         * 结束时间
         */
        private String endDate;

        /**
         * ExceptionLog表主键ID
         */
        private List<Integer> ids;
    }
}
